package com.ice.app.project.${objName}.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.ice.app.project.entity.${className};
import com.ice.app.project.mapper.${className}Mapper;
import com.iceframework.core.utils.KeyUtils;
import com.iceframework.core.utils.WebUtils;

/**
 * 
 * 业务逻辑层： ${comments}
 * 
 * @author ${author}
 * @email ${email}
 * @date ${datetime}
 */
@Service
public class ${className}Service {

	@Autowired
	private ${className}Mapper ${objName}Mapper;

	// ----------------------------------------自动生成--------------------------------------------

	/**
	 * 菜单保存
	 * 
	 * @param ${objName}
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean save${className}(${className} ${objName}) throws Exception {
		if (StringUtil.isNotEmpty(${objName}.get<#list pk as p>${p.attrName}</#list>())) {
			// 修改操作
			${className} old = this.get${className}(${objName}.get<#list pk as p>${p.attrName}</#list>());
			${objName}.setUpdateBy(WebUtils.getUserName());
			${objName}.setUpdateDate(new Date());
			if (old != null) {
				return ${objName}Mapper.updateByPrimaryKeySelective(${objName}) > 0;
			}
			return false;
		} else {
			// 保存操作
			String key = KeyUtils.genKey("${className}");
			${objName}.set<#list pk as p>${p.attrName}</#list>(key);
			${objName}.setCreateBy(WebUtils.getUserName());
			${objName}.setCreateDate(new Date());
			${objName}.setUpdateBy(WebUtils.getUserName());
			${objName}.setUpdateDate(new Date());
			return ${objName}Mapper.insertSelective(${objName}) > 0;

		}
	}

	/**
	 * 根据主键删除菜单
	 * 
	 * @param key
	 * @return
	 */
	@Transactional
	public boolean delete${className}(String key) {
		return ${objName}Mapper.deleteByPrimaryKey(key) > 0;
	}

	/**
	 * 根据多个主键删除菜单
	 * 
	 * @param keys
	 * @return
	 */
	@Transactional
	public boolean delete${className}s(String keys) {
		if (!StringUtils.isEmpty(keys)) {
			String[] strs = keys.split(",");
			for (String key : strs) {
				this.delete${className}(key);
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public ${className} get${className}(String key) {
		return ${objName}Mapper.selectByPrimaryKey(key);
	}

	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<${className}> get${className}s(${className} ${objName}) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("${objName}", ${objName});
		return ${objName}Mapper.selectByFilter(map);
	}

	// ----------------------------------------自定义（Public）---------------------------------------------

}